// schema扩展相关文档请参阅：https://uniapp.dcloud.net.cn/uniCloud/jql-schema-ext.html
module.exports = {   
   trigger: {
       beforeCreate: async function({
         collection,
         operation,
         addDataList,
         clientInfo
       } = {}) {
          const shopId = addDataList[0].shop_id;
		  const categoryId = addDataList[0].category_id;
		  const db = uniCloud.database();
		  const name= addDataList[0].name
		  const res = await db.collection('own-goods').where({name:name,shop_id:shopId}).get({
		  	getOne:true
		  });
		  if(res.data[0]){
		  	throw new Error('该店铺相同名称的商品已经存在，请勿重复添加！');
		  }
		  //增加分类的商品数量
		 await db.collection('own-shop-categories').doc(categoryId).update({
		    goods_number: db.command.inc(1)
		})
		//增加店铺的商品数量
		await db.collection('own-shop').doc(shopId).update({
		    goods: db.command.inc(1)
		})
		
       },
	}
}
